import '_base_export.dart';

class AppUtil {
  String last_version = '';
  bool force_update = false;
  Map popup_window = {};
  List category_list = [];
  Map creditcard = {};
  Map user_addr = {};

  String cs_cell = '';
  Map data = {};
  List vouchers = [];
  List membership = [];
  List wh_list = [];
  int wholesale_wh_id = 0;
  Map referral_rank = {};

  clear() async {
    creditcard = {};
    user_addr = {};
    data = {};
    vouchers = [];
    membership = [];
    wh_list = [];
    await initLoad();
  }

  initLoad() async {
    var lv_ad_region = (AppSession.wh_id != 0) ? AppSession.wh_id : 1;
    Map<String, dynamic> lm_header = {"PD-ADREGION": lv_ad_region};
    var lm_initapp = await AppSession.appReq.futureWrapper(
      ApiConst.httpGet,
      ApiServer.prod + ApiUri.init_app,
      lm_header,
      {},
      true, //(AppSession.authortoken ?? '').isNotEmpty,
      false,
    );
    if ((lm_initapp['ev_result'] ?? {}).isNotEmpty) {
      var lm_result = lm_initapp['ev_result'];
      print(lm_result['category_list']);

      if ((lm_result['latest_version'] ?? '').isNotEmpty)
        last_version = lm_result['latest_version'];
      if (lm_result['force_update'] != null) force_update = lm_result['force_update'];
      if ((lm_result['popup_window'] ?? {}).isNotEmpty) popup_window = lm_result['popup_window'];
      if ((lm_result['user_address'] ?? {}).isNotEmpty) {
        AppSession.userAddr = lm_result['user_address'];
      }

      if ((lm_result['payment_cards'] ?? {}).isNotEmpty){
        if ((lm_result['payment_cards']['2'] ?? {}).isNotEmpty){
          AppSession.creditcard = lm_result['payment_cards']['2'];
        }
      }
      if ((lm_result['category_list'] ?? []).isNotEmpty) category_list = lm_result['category_list'];
      if ((lm_result['cs_cell'] ?? '').isNotEmpty) cs_cell = lm_result['cs_cell'];
      if ((lm_result['vip_cards'] ?? []).isNotEmpty) membership = lm_result['vip_cards'];
      if ((lm_result['wh_list'] ?? []).isNotEmpty) wh_list = lm_result['wh_list'];
      if (lm_result['wholesale_wh_id'] != null) wholesale_wh_id = lm_result['wholesale_wh_id'];
      data = lm_result;
    }

    await loadVouchers({});
    await referalRank();
  }

  paymentMaintain(String iv_pay_method) async {
    var lm_result = await AppSession.appReq.futureWrapper(
      ApiConst.httpPost,
      ApiServer.prod + ApiUri.add_payment_method,
      {},
      {"payment_method_id": iv_pay_method},
      true,
      true,
    );
    if (lm_result['ev_error'] == 0)
      return lm_result;
    else if ((lm_result['ev_message'] ?? '').isNotEmpty) {
      AppPopupService.showAlertSimple('E', '出错啦', lm_result['ev_message']);
      return {};
    }
  }

  loadVouchers(Map<String, dynamic> im_param) async {
    Map<String, dynamic> lm_param = {};
    lm_param.addAll(im_param);

    var lm_result = await AppSession.appReq.futureWrapper(
      ApiConst.httpPost,
      ApiServer.prod + ApiUri.get_vouchers,
      // ApiServer.prod + ApiUri.voucher_history,
      {},
      lm_param,
      true,
      false,
    );

    if (lm_result['ev_error'] == 0) {
      vouchers = lm_result['ev_result'];
      return lm_result;
    } else if ((lm_result['ev_message'] ?? '').isNotEmpty) {
      AppPopupService.showAlertSimple('E', '出错啦', lm_result['ev_message']);
      return {};
    }
  }

  orderRepay(Map<String, dynamic> im_param) async {
    Map<String, dynamic> lm_param = {};
    lm_param.addAll(im_param);

    var lm_result = await AppSession.appReq.futureWrapper(
      ApiConst.httpPost,
      ApiServer.prod + ApiUri.order_repay,
      {},
      lm_param,
      true,
      true,
    );

    if (lm_result['ev_error'] == 0)
      return lm_result;
    else if ((lm_result['ev_message'] ?? '').isNotEmpty) {
      AppPopupService.showAlertSimple('E', '出错啦', lm_result['ev_message']);
      return {};
    }
  }

  wechatLogin(Map<String, dynamic> im_param) async {
    Map<String, dynamic> lm_param = {};
    lm_param.addAll(im_param);

    var lm_result = await AppSession.appReq.futureWrapper(
      ApiConst.httpPost,
      ApiServer.prod + ApiUri.wechat_login,
      {},
      lm_param,
      true,
      true,
    );
    print(lm_result);
    if (lm_result['ev_error'] == 0)
      return lm_result;
    else if ((lm_result['ev_message'] ?? '').isNotEmpty) {
      AppPopupService.showAlertSimple('E', '出错啦', lm_result['ev_message']);
      return {};
    }
  }

  wholesaleApply(Map<String, dynamic> im_param) async {
    Map<String, dynamic> lm_param = {};
    lm_param.addAll(im_param);

    var lm_result = await AppSession.appReq.futureWrapper(
      ApiConst.httpPost,
      ApiServer.prod + ApiUri.apply_wholesale, //ApiUri.group_apply,
      {},
      lm_param,
      true,
      true,
    );
    print(lm_result);
    if (lm_result['ev_error'] == 0)
      return lm_result;
    else if ((lm_result['ev_message'] ?? '').isNotEmpty) {
      AppPopupService.showAlertSimple('E', '出错啦', lm_result['ev_message']);
      return {};
    }
  }

  referalRank() async {
    // Map<String, dynamic> lm_param = {};Map<String, dynamic> im_param
    // lm_param.addAll(im_param);

    var lm_result = await AppSession.appReq.futureWrapper(
      ApiConst.httpGet,
      ApiServer.prod + ApiUri.referal_rank, //ApiUri.group_apply,
      {},
      {},
      true,
      true,
    );
    print(lm_result);
    if (lm_result['ev_error'] == 0) {
      referral_rank['rank'] = lm_result['ev_result']['rank'];
      referral_rank['list'] = lm_result['ev_result']['topN'];
      referral_rank['setting'] = lm_result['ev_result']['reward_setting'];
      // return lm_result;
    }

    // else if ((lm_result['ev_message'] ?? '').isNotEmpty) {
    //   AppPopupService.showAlertSimple('E', '出错啦', lm_result['ev_message']);
    //   return {};
    // }
  }
}
